/**
 * Created by FreezeSoul on 2016/3/27.
 */
define([
    "app/core/base",
    "app/core/util",
    "../commonMeta",
    "jquery",
    "underscore",
    'JSONParse',
    'loglevel',
    'jqueryTmpl'
], function (Chart, Util, Meta, $, _, JSON, log) {

    var property = {
        config: "property",
        option: [
            {
                group: '基础',
                id: 'HTMLDaiMaPianDuan',
                name: 'HTML代码片段',
                enable: true,
                type: 'textarea',
                value: '',
                default: '',
                link: 'htmlTemplate',
                tooltip: 'HTML模板代码片段，样式请采用内联方式，支持数据集模板字段，如:{字段名}'
            },
            {
                group: '基础',
                id: 'HTMLHoverAnimationList',
                name: '鼠标响应',
                enable: false,
                type: 'select',
                value: '',
                default: '',
                data: [{
                    value: '',
                    text: '无效果'
                }, {value: 'bounce', text: 'bounce'},
                    {value: 'flash', text: 'flash'},
                    {value: 'pulse', text: 'pulse'},
                    {value: 'rubberBand', text: 'rubberBand'},
                    {value: 'shake', text: 'shake'},
                    {value: 'headShake', text: 'headShake'},
                    {value: 'swing', text: 'swing'},
                    {value: 'tada', text: 'tada'},
                    {value: 'wobble', text: 'wobble'},
                    {value: 'jello', text: 'jello'},
                    {value: 'bounceIn', text: 'bounceIn'},
                    {value: 'bounceInDown', text: 'bounceInDown'},
                    {value: 'bounceInLeft', text: 'bounceInLeft'},
                    {value: 'bounceInRight', text: 'bounceInRight'},
                    {value: 'bounceInUp', text: 'bounceInUp'},
                    {value: 'bounceOut', text: 'bounceOut'},
                    {value: 'bounceOutDown', text: 'bounceOutDown'},
                    {value: 'bounceOutLeft', text: 'bounceOutLeft'},
                    {value: 'bounceOutRight', text: 'bounceOutRight'},
                    {value: 'bounceOutUp', text: 'bounceOutUp'},
                    {value: 'fadeIn', text: 'fadeIn'},
                    {value: 'fadeInDown', text: 'fadeInDown'},
                    {value: 'fadeInDownBig', text: 'fadeInDownBig'},
                    {value: 'fadeInLeft', text: 'fadeInLeft'},
                    {value: 'fadeInLeftBig', text: 'fadeInLeftBig'},
                    {value: 'fadeInRight', text: 'fadeInRight'},
                    {value: 'fadeInRightBig', text: 'fadeInRightBig'},
                    {value: 'fadeInUp', text: 'fadeInUp'},
                    {value: 'fadeInUpBig', text: 'fadeInUpBig'},
                    {value: 'fadeOut', text: 'fadeOut'},
                    {value: 'fadeOutDown', text: 'fadeOutDown'},
                    {value: 'fadeOutDownBig', text: 'fadeOutDownBig'},
                    {value: 'fadeOutLeft', text: 'fadeOutLeft'},
                    {value: 'fadeOutLeftBig', text: 'fadeOutLeftBig'},
                    {value: 'fadeOutRight', text: 'fadeOutRight'},
                    {value: 'fadeOutRightBig', text: 'fadeOutRightBig'},
                    {value: 'fadeOutUp', text: 'fadeOutUp'},
                    {value: 'fadeOutUpBig', text: 'fadeOutUpBig'},
                    {value: 'flipInX', text: 'flipInX'},
                    {value: 'flipInY', text: 'flipInY'},
                    {value: 'flipOutX', text: 'flipOutX'},
                    {value: 'flipOutY', text: 'flipOutY'},
                    {value: 'lightSpeedIn', text: 'lightSpeedIn'},
                    {value: 'lightSpeedOut', text: 'lightSpeedOut'},
                    {value: 'rotateIn', text: 'rotateIn'},
                    {value: 'rotateInDownLeft', text: 'rotateInDownLeft'},
                    {value: 'rotateInDownRight', text: 'rotateInDownRight'},
                    {value: 'rotateInUpLeft', text: 'rotateInUpLeft'},
                    {value: 'rotateInUpRight', text: 'rotateInUpRight'},
                    {value: 'rotateOut', text: 'rotateOut'},
                    {value: 'rotateOutDownLeft', text: 'rotateOutDownLeft'},
                    {value: 'rotateOutDownRight', text: 'rotateOutDownRight'},
                    {value: 'rotateOutUpLeft', text: 'rotateOutUpLeft'},
                    {value: 'rotateOutUpRight', text: 'rotateOutUpRight'},
                    {value: 'hinge', text: 'hinge'},
                    {value: 'jackInTheBox', text: 'jackInTheBox'},
                    {value: 'rollIn', text: 'rollIn'},
                    {value: 'rollOut', text: 'rollOut'},
                    {value: 'zoomIn', text: 'zoomIn'},
                    {value: 'zoomInDown', text: 'zoomInDown'},
                    {value: 'zoomInLeft', text: 'zoomInLeft'},
                    {value: 'zoomInRight', text: 'zoomInRight'},
                    {value: 'zoomInUp', text: 'zoomInUp'},
                    {value: 'zoomOut', text: 'zoomOut'},
                    {value: 'zoomOutDown', text: 'zoomOutDown'},
                    {value: 'zoomOutLeft', text: 'zoomOutLeft'},
                    {value: 'zoomOutRight', text: 'zoomOutRight'},
                    {value: 'zoomOutUp', text: 'zoomOutUp'},
                    {value: 'slideInDown', text: 'slideInDown'},
                    {value: 'slideInLeft', text: 'slideInLeft'},
                    {value: 'slideInRight', text: 'slideInRight'},
                    {value: 'slideInUp', text: 'slideInUp'},
                    {value: 'slideOutDown', text: 'slideOutDown'},
                    {value: 'slideOutLeft', text: 'slideOutLeft'},
                    {value: 'slideOutRight', text: 'slideOutRight'},
                    {value: 'slideOutUp', text: 'slideOutUp'}],
                link: 'hoverAnimation',
                tooltip: '鼠标响应动画'
            }, {
                group: '绑定',
                id: 'FilterName',
                name: '过滤列名称',
                enable: false,
                type: 'text',
                value: '',
                default: '',
                link: 'filterName',
                tooltip: '绑定数据集，客户端过滤字段列名称'
            },
            {
                group: '绑定',
                id: 'FilterValue',
                name: '过滤列默认值',
                enable: false,
                type: 'text',
                value: '',
                default: '',
                link: 'filterValue',
                tooltip: '绑定数据集，客户端过滤字段默认值'
            }
        ].concat($.extend(true, [], Meta.commonProperties)),
        MapOption: function (option) {
            Util.linkOption(this.option, option);
        }
    };

    var html = Chart.extend({
        constructor: function (layout) {
            this._layout = layout;
            this._element = null;
            this._container = null;
            this._lastdata = null;

            this._option = {
                Series: [],
                Events: [],
                Extend: $.extend(true, {}, property),
                Option: {
                    uuid: '',
                    class: '',
                    display: true,
                    htmlTemplate: '',
                    hoverAnimation: "",
                    initScript: '',
                    disposeScript: ''
                }
            };
        },
        init: function (element) {
            try {
                this._container = element;
            } catch (e) {
                log.error(e);
            }
        },
        _readProperty: function () {
            try {
                if (!this._option.Extend) return;
                Util.deepMapOption(property, this._option.Extend, this._option.Option);
            } catch (e) {
                log.error(e);
            }
        },
        _bindEvent: function () {
            var _this = this;
            if (!this._filterDataHandle) {
                this._filterDataHandle = function (params) {
                    _this.filterDataHandle.call(_this, params);
                };
            }
            this._layout.removeListener("filterData", this._filterDataHandle);
            this._layout.addListener("filterData", this._filterDataHandle);
        },
        filterDataHandle: function (params) {
            var _this = this;
            var flag = false;
            var filterColumn = _.findWhere(this._option.Extend.option, {name: '过滤列名称'});
            var filterValue = _.findWhere(this._option.Extend.option, {name: '过滤列默认值'});

            if (filterColumn && filterValue
                && filterColumn.enable && filterValue.enable
                && filterColumn.value !== "" && filterValue.value !== ""
            ) {
                for (var prop in params) {
                    if (!params.hasOwnProperty(prop)) continue;
                    if (filterColumn.value === prop) {
                        filterValue.value = params[prop];
                        flag = true;
                    }
                }
            }
            if (flag && _this._lastdata) {
                _this.setData(_this._lastdata);
            }
        },
        _render: function (template, data) {
            var $element = $(template);
            if ($.isArray(data) && data.length > 0) {
                $element = $.tmpl(template, data);
            }
            $(this._container).empty();
            $element.appendTo(this._container);

            if (this._option.Option.hoverAnimation) {
                var _this = this;
                $(this._container).hover(
                    function () {
                        if ($(_this._container).hasClass("animated")) return;
                        $(_this._container).addClass("animated " + _this._option.Option.hoverAnimation)
                            .one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function () {
                                $(_this._container).removeClass("animated " + _this._option.Option.hoverAnimation)
                            });
                    }, function () {
                    }
                );
            }

            //Util.setCommonMeta(this, this._container, this._option.Option);
            //Util.initChartLifeTime(this, this._container, this._option.Option);

            return $element;
        },
        example: function () {
            try {
                this._element = this._render(this._option.Option.htmlTemplate);
            } catch (e) {
                log.error(e);
            }
        },
        _filterTable: function (option, table) {
            var filterColumn = _.findWhere(option, {name: '过滤列名称'});
            var filterValue = _.findWhere(option, {name: '过滤列默认值'});

            if (table && filterColumn && filterValue
                && filterColumn.enable && filterValue.enable
                && filterColumn.value !== "" && filterValue.value !== ""
            ) {
                var tableCopy = _.map(table, _.clone);
                return _.filter(tableCopy, function (row) {
                    return row[filterColumn.value] === filterValue.value;
                });
            }
            return table;
        },
        setData: function (data) {
            try {
                this._lastdata = data;

                this._readProperty();
                var arrayData = [];
                if (data
                    && this._option.Binding
                    && this._option.Binding.length > 0
                    && this._option.Extend
                ) {
                    var binding = this._option.Binding[0];
                    var table = data[binding.DataSetCode];
                    table = this._filterTable(this._option.Extend.option, table);
                    if (table && table.length > 0) {
                        $.each(table, function (i, row) {
                            var obj = {};
                            _.each(binding.Measures, function (measure) {
                                obj[measure.Name] = row[measure.Column];
                            });

                            _.each(binding.Dimensions, function (dimension) {
                                obj[dimension.Name] = row[dimension.Column];
                            });

                            obj["index"] = i;
                            arrayData.push(obj);
                        });
                    }
                }
                this._element = this._render(this._option.Option.htmlTemplate, arrayData);
                this._bindEvent();
            } catch (e) {
                log.error(e);
            }
        },
        setOption: function (option) {
            try {
                return this._option = $.extend(true, {}, option);
            } catch (e) {
                log.error(e);
            }
        },
        getOption: function () {
            try {
                this._readProperty();
            } catch (e) {
                log.error(e);
            }
            return this._option;
        },
        setTheme: function (theme) {
            try {
            } catch (e) {
                log.error(e);
            }
        },
        resize: function () {
            try {
            } catch (e) {
                log.error(e);
            }
        },
        showLoading: function () {
            try {
                if (this._layout && this._layout.showLoading) {
                    this._layout.showLoading(this);
                }
            } catch (e) {
                log.error(e);
            }
        },
        hideLoading: function () {
            try {
                if (this._layout && this._layout.hideLoading) {
                    this._layout.hideLoading(this);
                }
            } catch (e) {
                log.error(e);
            }
        },
        dispose: function () {
            try {
                $(this._element).off().remove();
                this._layout.removeListener("filterData", this._filterDataHandle);
                //Util.disposeChartLifeTime(this, this._container, this._option.Option);

            } catch (e) {
                log.error(e);
            }
        },
        getElement: function () {
            return this._element;
        },
        getChart: function () {
            return this._element;
        }
    });

    return {
        Chart: html,
        Property: property
    };
});